package com.qiezi.project.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qiezi.R;

public class PriceView extends RelativeLayout {

    public PriceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs,0);
    }


    private String title = "",hint = "",newPrice = "",oldPrice = "";
    private boolean isRecommend = false;


    private TextView tvTitle,tvHint,tvNewPrice,tvOldPrice;
    private ImageView ivRecommend;

    private void init(Context context,AttributeSet attributeSet,int paramInt){
        LayoutInflater.from(context).inflate(R.layout.view_price, this);
        if (attributeSet != null){
            TypedArray typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.PriceView, paramInt, 0);
            title = typedArray.getString(R.styleable.PriceView_pv_title);
            hint = typedArray.getString(R.styleable.PriceView_pv_hint);
            newPrice = typedArray.getString(R.styleable.PriceView_pv_newPrice);
            oldPrice = typedArray.getString(R.styleable.PriceView_pv_oldPrice);
            isRecommend = typedArray.getBoolean(R.styleable.PriceView_pv_isRecommend,isRecommend);
            typedArray.recycle();
        }

        tvTitle = findViewById(R.id.tv_price_title);
        tvHint = findViewById(R.id.tv_hint);
        tvNewPrice = findViewById(R.id.tv_price_new);
        ivRecommend = findViewById(R.id.iv_recommend);
        tvOldPrice = findViewById(R.id.tv_price_old);


        tvOldPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);
        tvOldPrice.getPaint().setAntiAlias(true);

        if (isRecommend){
            ivRecommend.setVisibility(VISIBLE);
        }else {
            ivRecommend.setVisibility(GONE);
        }
        tvTitle.setText(title);
        tvHint.setText(hint);
        tvOldPrice.setText(oldPrice);
        tvNewPrice.setText(newPrice);
    }

    public void setRecommend(boolean recommend){
        if (ivRecommend == null)return;
        ivRecommend.setVisibility(recommend?VISIBLE:GONE);
    }

    public void setTitle(String title) {
        this.title = title;
        if (tvTitle == null)return;
        tvTitle.setText(title);
    }

    public void setHint(String hint) {
        this.hint = "("+hint+")";
        if (tvHint == null)return;
        tvHint.setText(this.hint);
    }

    public void setNewPrice(String newPrice) {
        this.newPrice = "￥"+newPrice;
        if (tvNewPrice == null)return;
        tvNewPrice.setText(this.newPrice);
    }

    public void setOldPrice(String oldPrice) {
        this.oldPrice = "￥"+oldPrice;
        if (tvOldPrice == null)return;
        tvOldPrice.setText(this.oldPrice);
    }
}
